import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken, setuser, getuser, removeuser, getUserId, setUserId, getRoleGroup, setRoleGroup, setUserDic, getUserDic } from '@/utils/auth'
import { Message, MessageBox } from 'element-ui'
const dataList = { //临时权限路由分配
    "role": [{
            "path": "/home/index",
            "hidden": false,
            "name": "系统首页",
            "hips": true,
            "transEng": "popoposss",
            "icon": "fa-home",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": "",
        },
        {
            "path": "/firstManage/list",
            "hidden": true,
            "hips": true,
            "name": "初筛管理",
            "transEng": "popoposss",
            "icon": "fa-shopping-bag",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": "",
            "children": [{
                "path": "/firstManage/detail",
                "hidden": true,
                "name": "编辑",
                "hips": true,
                "transEng": "popoposss",
                "icon": null,
                "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
                "parentId": "",
            }]
        },
        {
            "path": "/participant/list",
            "hidden": false,
            "name": "研究对象",
            "hips": true,
            "transEng": "popoposss",
            "icon": "fa-key",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": "",
            "children": [{
                    "path": "/participant/add",
                    "hidden": false,
                    "name": "添加",
                    "hips": true,
                    "transEng": "popoposss",
                    "icon": null,
                    "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
                    "parentId": "",
                },
                {
                    "path": "/participant/changePeriod",
                    "hidden": true,
                    "name": "转组档案",
                    "hips": true,
                    "transEng": "popoposss",
                    "icon": "fa-key",
                    "id": "5t7ub81e-54ad-48d8-b",
                    "parentId": "",
                },
                {
                    "path": "/participant/editor",
                    "hidden": true,
                    "name": "查看研究对象",
                    "hips": true,
                    "transEng": "popoposss",
                    "icon": "fa-key",
                    "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
                    "parentId": "",
                }
            ]
        }, {
            "path": "/fllowManage/list",
            "hidden": false,
            "name": "健康访谈",
            "hips": true,
            "transEng": "popoposss",
            "icon": "fa-comment",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": "",

        },
        {
            "path": "/healthManage/list",
            "hidden": false,
            "name": "健教管理",
            "hips": true,
            "transEng": "popoposss",
            "icon": "fa-gg-circle",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": "",
        },
        {
            "path": "/dataColleManage/list",
            "hidden": false,
            "name": "数据采集",
            "hips": true,
            "transEng": "popoposss",
            "icon": "fa-cubes",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": "",
        },
        {
            "path": "/nutClinic/list",
            "hidden": false,
            "name": "营养门诊",
            "hips": true,
            "transEng": "popoposss",
            "icon": "fa-indent",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": "",
        },
        {
            "path": "/brestFeeding/list",
            "hidden": false,
            "name": "母乳喂养",
            "hips": true,
            "transEng": "popoposss",
            "icon": "fa-diamond",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": "",
        },
        {
            "path": "/obesityManage/list",
            "hidden": false,
            "name": "肥胖门诊",
            "hips": true,
            "transEng": "popoposss",
            "icon": "fa-indent",
            "id": "5t7ub81e-54ad-48d8sbad9-7d2d4a2ddes3",
            "parentId": "",
        },
        {
            "path": "/satisfation/list",
            "hidden": false,
            "name": "满意度调查",
            "transEng": "popoposss",
            "hips": true,
            "icon": "fa-star-half-o",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": ""
        },
        {
            "path": "/ZkMange/list",
            "hidden": false,
            "name": "访谈质控",
            "transEng": "popoposss",
            "hips": true,
            "icon": "fa-archive",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": ""
        },
        {
            "path": "/scheduleTemp/list",
            "hidden": false,
            "name": "日程模板",
            "hips": true,
            "transEng": "popoposss",
            "icon": "fa-info-circle",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": ""
        }, {
            "path": "/questionTemp/list",
            "hidden": false,
            "name": "问卷模板",
            "transEng": "popoposss",
            "hips": true,
            "icon": "fa-circle",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": ""
        }, {
            "path": "/dicManage/list",
            "hidden": false,
            "name": "字典管理",
            "transEng": "popoposss",
            "hips": true,
            "icon": "fa-reorder",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": ""
        },
        {
            "path": "/lifeMessenger/list",
            "hidden": false,
            "name": "生命树信使",
            "transEng": "popoposss",
            "hips": true,
            "icon": "fa-heartbeat",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": ""
        },
        {
            "path": "/fileManage/list",
            "hidden": false,
            "name": "文件管理",
            "transEng": "popoposss",
            "hips": true,
            "icon": "fa-cloud-upload",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": ""
        },
        {
            "path": "/statistics/list",
            "hidden": false,
            "name": "统计分析",
            "transEng": "popoposss",
            "hips": true,
            "icon": "fa-area-chart",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": ""
        },
        {
            "path": "/infoManage/list",
            "hidden": false,
            "name": "消息管理",
            "transEng": "popoposss",
            "hips": true,
            "icon": "fa-comment",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": ""
        },
        {
            "path": "/logManage/list",
            "hidden": false,
            "name": "日志管理",
            "transEng": "popoposss",
            "hips": true,
            "icon": "fa-reorder",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": ""
        },
        {
            "path": "/userManage/list",
            "hidden": false,
            "name": "账号管理",
            "transEng": "popoposss",
            "hips": true,
            "icon": "fa-user",
            "id": "5t7ub81e-54ad-48d8-bad9-7d2d4a2ddes3",
            "parentId": ""
        },
    ],
    "permissionsList": [
        "sys.operatemenu.list",
        "sys.operatemenu.add",
        "sys.operatemenubtn.list",
    ]
}
const user = {
    state: {
        token: getToken(),
        name: '',
        sex: '',
        avatar: '',
        infoNews: false,
        roles: [],
        UserDic: getUserDic(),
        userIcon: getuser(),
        resechObj: {}, //研究对象
        userId: getUserId(),
        RoleGroup: getRoleGroup()
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_SEX: (state, sex) => {
            state.sex = sex
        },
        SET_IMG: (state, userIcon) => {
            state.userIcon = userIcon
        },
        SET_RESECHOBJ: (state, resechObj) => {
            state.resechObj = resechObj
        },
        SET_INFORNEWS: (state, infoNews) => {
            state.infoNews = infoNews
        },
        SET_RoleGroup: (state, RoleGroup) => {
            state.RoleGroup = RoleGroup
        },
        SET_UserDic: (state, UserDic) => {
            state.UserDic = UserDic
        },
    },

    actions: {
        // 登录
        Login({ commit }, userInfo) { //userinfo
            const username = userInfo.username.trim()
            return new Promise((resolve, reject) => {
                login(username, userInfo.password).then(response => {
                    if (response.dtData[0].IsStopUse == '1') {
                        reject('该账号已停用') //否决
                        return false
                    }
                    let data = response.dtData[0]
                    data.Name = data.Name || ''
                    let username = data.Surname + data.Name
                    username.indexOf('undefined') > -1 ? username = data.UserCode : username = username
                    setuser(username)
                    setUserDic(JSON.stringify(data))
                    setToken(data.SYS_ID)
                    setUserId(data.SYS_ID) // 设置当前登录用户的Id
                    setRoleGroup(data.RoleGroup)
                    commit('SET_IMG', username)
                    commit('SET_UserDic', JSON.stringify(data)) //main
                    commit('SET_RoleGroup', data.RoleGroup)
                    commit('SET_TOKEN', data.SYS_ID)
                    resolve() //完成
                }).catch(error => {
                    setuser(1)
                    setUserDic('{}')
                    setToken(1)
                    setUserId(1) // 设置当前登录用户的Id
                    setRoleGroup(1)
                    commit('SET_IMG', 1)
                    commit('SET_UserDic', '{}') //main
                    commit('SET_RoleGroup', 1)
                    commit('SET_TOKEN', 1)
                    resolve(error) //否决
                })
            })
        },
        /*****************
         * 18  日志管理   19账号管理  16统计分析  15文件管理  14生命树信使   13字典管理  8肥胖门诊
         * 12问卷模板    11日程模板   10访谈质控    9满意度调查 7哺乳喂养   6营养门诊  5数据采集  4键教管理   3健康访谈  2研究对象   1初筛管理  0系统首页
         */
        // 获取用户信息
        GetInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                const data = dataList; //为了加快项目时间,权限菜单管理暂时未做
                if (parseInt(getRoleGroup()) > 2) { //前端隐藏后四个菜单，其实还可以通过地址访问，后续接口动态获取(1-系统管理员2-项目经理3-健康访谈人员
                    // 4-APP职员,5:营养师，6健康教育,7Hcp干预组，8HCP对照组,9哺乳顾问,10数据采集)
                    data.role[9].hidden = true
                    data.role[11].hidden = true
                    data.role[12].hidden = true
                    data.role[13].hidden = true
                    data.role[15].hidden = true
                    data.role[17].hidden = true
                    data.role[18].hidden = true
                    data.role[19].hidden = true
                    if (parseInt(getRoleGroup()) == 3 || parseInt(getRoleGroup()) == 4 || parseInt(getRoleGroup()) == 10) { //健康访谈和app职员
                        data.role[10].hidden = true
                        data.role[13].hidden = true
                        data.role[14].hidden = true
                    } else if (parseInt(getRoleGroup()) == 5) { //营养师
                        data.role[0].hidden = true
                        data.role[3].hidden = true
                        data.role[4].hidden = true
                        data.role[10].hidden = true
                        data.role[14].hidden = true
                        data.role[16].hidden = true
                    } else if (parseInt(getRoleGroup()) == 6) { //健康教育
                        data.role[0].hidden = true
                        data.role[2].hidden = true
                        data.role[3].hidden = true
                        data.role[6].hidden = true
                        data.role[10].hidden = true
                    } else if (parseInt(getRoleGroup()) == 7) { //Hcp干预组
                        data.role[4].hidden = true
                        data.role[5].hidden = true
                        data.role[6].hidden = true
                        data.role[14].hidden = true
                        data.role[16].hidden = true
                        data.role[10].hidden = true
                    } else if (parseInt(getRoleGroup()) == 8) { //Hcp对照组
                        data.role[0].hidden = true
                        data.role[4].hidden = true
                        data.role[5].hidden = true
                        data.role[3].hidden = true
                        data.role[6].hidden = true
                        data.role[7].hidden = true
                        data.role[8].hidden = true
                        data.role[10].hidden = true
                        data.role[14].hidden = true
                        data.role[16].hidden = true
                    } else if (parseInt(getRoleGroup()) == 9) { //哺乳顾问
                        data.role[0].hidden = true
                        data.role[2].hidden = true
                        data.role[4].hidden = true
                        data.role[3].hidden = true
                        data.role[5].hidden = true
                        data.role[6].hidden = true
                        data.role[8].hidden = true
                        data.role[10].hidden = true
                        data.role[14].hidden = true
                        data.role[16].hidden = true
                    } else if (parseInt(getRoleGroup()) == 12 || parseInt(getRoleGroup()) == 13) { //内外部评估员
                        data.role[0].hidden = true
                        data.role[2].hidden = true
                        data.role[4].hidden = true
                        data.role[3].hidden = true
                        data.role[5].hidden = true
                        data.role[6].hidden = true
                        data.role[7].hidden = true
                        data.role[8].hidden = true
                        data.role[14].hidden = true
                        data.role[16].hidden = true
                    }
                }
                commit('SET_ROLES', data.role);
                resolve(data)
            })
        },
        // 设置头像
        SetHeadImg({ commit }, headImg) {
            return new Promise(resolve => {
                commit('SET_IMG', headImg)
                resolve()
            })
        },
        // 登出
        LogOut({ commit, state }) {
            return new Promise((resolve, reject) => {
                commit('SET_TOKEN', '')
                commit('SET_ROLES', [])
                removeToken()
                resolve()
                removeuser()
            })
        },
        // 前端 登出
        FedLogOut({ commit }) {
            return new Promise(resolve => {
                commit('SET_TOKEN', '')
                removeToken()
                resolve()
            })
        }
    }
}

export default user